﻿package com.tpo.util;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

@SuppressWarnings("restriction")

public class StringUtil
{
	public static Date parseDate(String str,String format)
	{
		try{
			SimpleDateFormat sdf=new SimpleDateFormat(format);
			return sdf.parse(str);
		}
		catch(Exception e){throw new RuntimeException();}
	}
	
	public static String convertDate(Date date,String format)
	{
		try{
			SimpleDateFormat sdf=new SimpleDateFormat(format);
			return sdf.format(date);
		}
		catch(Exception e){throw new RuntimeException();}
	}
	
	//根据pattern得到str单独匹配的值
	public static String[] getSingleMatchValue(String str,String pattern)
	{
		Pattern p=Pattern.compile(pattern);
		Matcher m=p.matcher(str);
		int groupCount=m.groupCount();
		if(m.find())
		{
			String[] values=null;
			if(groupCount==0){//如果无分组
				values=new String[1];
				values[0]=m.group();
			}
			else{//如果有分组
				values=new String[groupCount];
				for(int i=0;i<values.length;i++)
					values[i]=m.group(i+1);
			}
			return values;
		}
		return null;
	}
	public static  List<String> stringConverList(String ids){
	        String d[] = ids.split(",");
	        List<String> list = new ArrayList<String>();
	        for (int i = 0; i < d.length; i++) {
	        	list.add(d[i]);
	        }
	        return list;
		
	}
	 
	//编码
	 public static String getBase64(String str) {  
		 
	        byte[] b = null;  
	        String s = null;  
	        try {  
	            b = str.getBytes("utf-8");  
	        } catch (UnsupportedEncodingException e) {  
	            e.printStackTrace();  
	        }  
	        if (b != null) {  
	            s = new BASE64Encoder().encode(b);  
	        }  
            System.out.println(s);
	        return s;  
	    }  
	
	 
	      // 解密  
	    public static String getFromBase64(String s) {  
	        byte[] b = null;  
	        String result = null;  
	        if (s != null) {  
	            BASE64Decoder decoder = new BASE64Decoder();  
	            try {  
	                b = decoder.decodeBuffer(s);  
	                result = new String(b, "utf-8");  
	            } catch (Exception e) {  
	                e.printStackTrace();  
	            }  
	        }  
	        return result;  
	    }  
	
	 
	public static void main(String[] args) {
		
		String s = StringUtil.convertDate(new Date(),"yyyy-MM-dd  HH:mm:ss");
		
		System.out.println(s);
		
		Date date = new Date();
		
		System.out.println(date.getMonth()+1);
		
		System.out.println(date.getDate());
        	System.out.println("hello world!");
            System.out.println("test2!");
	}
	   
	   
}